package classes
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class Properties
	{
		private static var instance:Properties;
		private static var allowIstantiation:Boolean;
		private var connector:HTTPService;
		
		private var _filemanagerRoot:String;
		private var _cfcHome:String;
		private var _siteRoot:String;

	
		private var loaded:Boolean;
				
		public function Properties()
		{
			if(allowIstantiation){
				connector = new HTTPService();
				connector.url = 'assets/properties.xml';
				connector.resultFormat = "e4x";
				connector.addEventListener(ResultEvent.RESULT,propertiesRetrived);
				connector.addEventListener(FaultEvent.FAULT,function handleError(event:FaultEvent):void{
					trace("Error\n"+event.message);
				});
				trace("Properties - connector set, invoking remote call..");
				loaded = false;
				connector.send();
				allowIstantiation = false;
			}
		}
		
		private function propertiesRetrived(event:ResultEvent):void{
			trace("Properties - data retrived, parsing...");
			var result:XML = XML(event.result);
			_filemanagerRoot = result.filemanagerRoot;
			_cfcHome = "flexfilemanager.cfc.tmt_file_io";//result.cfcHome;
			_siteRoot = result.siteRoot;
			loaded = true;
			EventGateway.getInstance().dispatchPropertyChangedEvent();			
		}
		
		public static function getInstance():Properties{
			if(instance == null){
				trace("Properties - singleton not instantiated, running constructor");
				allowIstantiation = true;
				instance = new Properties();
			}
			return instance;
		}
		
		[Bindable]
		public function get filemanagerRoot():String{
			return _filemanagerRoot;
		}
		public function set filemanagerRoot(filemanagerRoot:String):void{
			this._filemanagerRoot = filemanagerRoot;
		}
		
		
		[Bindable]
		public function get cfcHome():String{
			return _cfcHome;
		}
		public function set cfcHome(cfcHome:String):void{
			this._cfcHome = cfcHome;
		}

		[Bindable]
		public function get siteRoot():String{
			return _siteRoot;
		}
		public function set siteRoot(siteRoot:String):void{
			this._siteRoot = siteRoot;
		}		

	}
}